﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThinhTien.Data;
using ThinhTien.Common;

namespace ThinhTien.Web.Controllers
{
    public class ProductsController : Controller
    {
        ProductRepository product = new ProductRepository();

        public ActionResult Details(string name, int id)
        {
            // retrieve the product from the database
            var productModel = product.GetProductById(id);

            // make sure the productName for the route matches the encoded product name
            string expectedName = productModel.Name.ToSeoUrl();
            string actualName = (name ?? "").ToLower();

            // permanently redirect to the correct URL
            if (expectedName != actualName)
            {
                return RedirectToActionPermanent("Details", "Products", new { name = expectedName, id = productModel.ProductID });
            }

            return View(productModel);
        }

    }
}
